package cn.ming.wfchat.app.me.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.dialog.PaymentDialog;
import cn.ming.wfchat.app.me.activity.adapter.AddressAdapter;
import cn.ming.wfchat.app.me.activity.model.ToWithdrawalBean;
import cn.ming.wfchat.kit.WfcBaseActivity;
import cn.ming.wfchat.kit.utils.Tools;
import cn.wildfirechat.remote.ChatManager;

/**
 * @author luoming
 * created at 2020/8/11 3:04 PM
 * 提现
 */
public class MyAssetsWithdrawalActivity extends WfcBaseActivity {


    @BindView(R.id.tv_keyong)
    TextView tvKeyong;
    @BindView(R.id.et_amount)
    EditText etAmount;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.tv_daozhang)
    TextView tvDaozhang;
    @BindView(R.id.tv_add)
    TextView tvAdd;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_shouxufei)
    TextView tvShouxufei;
    @BindView(R.id.tv_kyzc)
    TextView tvKyzc;
    @BindView(R.id.tv_sxf)
    TextView tvSxf;
    @BindView(R.id.tv_sjdz)
    TextView tvSjdz;

    private BigDecimal fee;
    private AddressAdapter addressAdapter;
    private List<ToWithdrawalBean.AddressListBean> listBeans;
    private String address;
    private String coinType;

    @Override
    protected int contentLayout() {
        return R.layout.my_assets_withdrawal_activity;
    }

    @Override
    protected void beforeViews() {
        super.beforeViews();
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        setTitle("提现");
        coinType = getIntent().getStringExtra("coinType");
        tvKyzc.setText("可用资产（" + coinType + "）");
        String undit = coinType.contains("ETH") ? "ETH" : "TRX";
        tvSxf.setText("手续费（" + undit + ")");
        tvSjdz.setText("实际到账（" + coinType + ")");
        tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tools.jump(MyAssetsWithdrawalActivity.this, MyAssetsWithdrawalAddressActivity.class, false);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        listBeans = new ArrayList<>();
        addressAdapter = new AddressAdapter(listBeans);
        View footview = LayoutInflater.from(this).inflate(R.layout.item_foot, null);
        footview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ChatManager.isPayPasswordEnable()) {
                    if (TextUtils.isEmpty(address)) {
                        ChatManager.showToast("请选择提现地址");
                    } else if (TextUtils.isEmpty(etAmount.getText().toString())) {
                        ChatManager.showToast("请输入提现金额");
                    } else {
                        withdrawal();
                    }

                } else {
                    ChatManager.showToast("您还没有设置资金密码，请先前往设置安全中设置");
                }
            }
        });
        addressAdapter.addFooterView(footview);
        addressAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (ToWithdrawalBean.AddressListBean addressListBean : listBeans) {
                    addressListBean.setSelected(false);
                }
                listBeans.get(position).setSelected(true);
                addressAdapter.notifyDataSetChanged();
            }
        });
        addressAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.iv_delete) {
                    removeWithdrawAddress(listBeans.get(position).getId() + "");
                }
            }
        });
        recyclerView.setAdapter(addressAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initDate();
    }

    void withdrawal() {
        PaymentDialog.newInstance("提现", "提现" + etAmount.getText().toString().trim() + coinType).setOnClickListener(new PaymentDialog.OnClickListener() {
            @Override
            public void getPwd(String pwd) {
                AppService.Instance().withdrawal(address, etAmount.getText().toString().trim(), coinType, pwd, new AppService.SendCodeCallback() {
                    @Override
                    public void onUiSuccess() {
                        ChatManager.showToast("操作成功");
                        finish();
                    }

                    @Override
                    public void onUiFailure(String msg) {
                        ChatManager.showToast(msg);
                    }
                });
            }
        }).show(this);

    }

    void removeWithdrawAddress(String id) {
        AppService.Instance().removeWithdrawAddress(id, new AppService.SendCodeCallback() {
            @Override
            public void onUiSuccess() {
                initDate();
                ChatManager.showToast("操作成功");

            }

            @Override
            public void onUiFailure(String msg) {
                ChatManager.showToast(msg);
            }
        });
    }


    void initDate() {
        AppService.Instance().toWithdrawal(coinType, new AppService.ToWithdrawalCallback() {
            @Override
            public void onUiSuccess(ToWithdrawalBean rechargeBean) {
                tvKeyong.setText(rechargeBean.getMyAssets().getBalance() + "");
                tvShouxufei.setText(rechargeBean.getMyAssets().getFee().toPlainString());
                fee = rechargeBean.getMyAssets().getFee();
                if (rechargeBean.getAddressList() != null && rechargeBean.getAddressList().size() > 0) {
                    listBeans = rechargeBean.getAddressList();
                    listBeans.get(0).setSelected(true);
                    address = listBeans.get(0).getAddress();
                    addressAdapter.setNewData(listBeans);
                }


            }

            @Override
            public void onUiFailure(int code, String msg) {
                ChatManager.showToast(msg);
            }
        });
    }

    @OnTextChanged(value = R.id.et_amount, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void inputAmoutumber(Editable editable) {
        if (fee == null)
            return;
        String amout = editable.toString().trim();
        try {
            BigDecimal amoutf = new BigDecimal(amout);
            tvDaozhang.setText(amoutf.subtract(fee) + "");
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }


    }

    @OnClick(R.id.tv_all)
    void setTvAll() {
        etAmount.setText(tvKeyong.getText().toString());
    }


}
